//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Text;

namespace OpenLS.Spreadsheet
{
    class TableLayoutInfo
    {
        public TableStyle Style { get; set; }
        public RangeAddress Range { get; set; }
        public bool ShowFirstColumn { get; set; }
        public bool ShowLastColumn { get; set; }
        public int HeaderRowCount { get; set; }
        public int TotalsRowCount { get; set; }
        public bool ShowRowStripes { get; set; }
        public bool ShowColumnStripes { get; set; }

        public TableLayoutInfo(TableStyle style, RangeAddress range, bool showFirstColumn, bool showLastColumn,
                               int headerRowCount, int totalsRowCount, bool showRowStripes, bool showColumnStripes)
        {
            Style = style;
            Range = range;
            ShowFirstColumn = showFirstColumn;
            ShowLastColumn = showLastColumn;
            HeaderRowCount = headerRowCount;
            TotalsRowCount = totalsRowCount;
            ShowRowStripes = showRowStripes;
            ShowColumnStripes = showColumnStripes;
        }
        public TableStyleElement GetElementFor(CellAddress cell, Predicate<DifferentialStyleRecord> predicate)
        {
            if (Style == null)
                return null;
            Predicate<TableStyleElement> filter = r => r != null && r.StyleRecord != null && predicate(r.StyleRecord);
            CellRelativeAddress ra = cell - Range.TopLeft;
            if (filter(Style.FirstHeaderCell) && (ra).Equals(new CellRelativeAddress(0, 0)))
                return Style.FirstHeaderCell;
            if (filter(Style.LastHeaderCell) && (ra).Equals(new CellRelativeAddress(0, Range.ColumnSpan - 1)))
                return Style.LastHeaderCell;
            if (filter(Style.FirstHeaderCell) && (ra).Equals(new CellRelativeAddress(0, 0)))
                return Style.FirstHeaderCell;
            if (filter(Style.FirstHeaderCell) && (ra).Equals(new CellRelativeAddress(0, 0)))
                return Style.FirstHeaderCell; //\\ tofinish
            int topRowIndex = Range.TopLeft.Row;
            int topColumnIndex = Range.TopLeft.Column;
            if (filter(Style.HeaderRow) && cell.Row >= topRowIndex && cell.Row < topRowIndex + HeaderRowCount)
                return Style.HeaderRow;
            if (filter(Style.TotalRow) && cell.Row >= topRowIndex + Range.RowSpan - TotalsRowCount)
                return Style.TotalRow;
            if (ShowFirstColumn && filter(Style.FirstColumn) && cell.Column == Range.TopLeft.Column)
                return Style.FirstColumn;
            if (ShowLastColumn && filter(Style.LastColumn) && cell.Column == Range.TopLeft.Column + Range.ColumnSpan - 1)
                return Style.LastColumn;
            {
                int firstRowStripeHeight = filter(Style.FirstRowStripe) ? Style.FirstRowStripe.Size : 1;
                int secondRowStripeHeight = filter(Style.SecondRowStripe) ? Style.SecondRowStripe.Size : 1;
                if (ShowRowStripes && filter(Style.FirstRowStripe))
                {
                    int rowDiff = cell.Row - topRowIndex - HeaderRowCount;
                    rowDiff = rowDiff % (firstRowStripeHeight + secondRowStripeHeight);
                    if (rowDiff < firstRowStripeHeight)
                        return Style.FirstRowStripe;
                }
                if (ShowRowStripes && filter(Style.SecondRowStripe))
                    return Style.SecondRowStripe;
            }
            {
                int firstColumnStripeHeight = filter(Style.FirstColumnStripe) ? Style.FirstColumnStripe.Size : 1;
                int secondColumnStripeHeight = filter(Style.SecondColumnStripe) ? Style.SecondColumnStripe.Size : 1;
                if (ShowColumnStripes && filter(Style.FirstColumnStripe))
                {
                    int columnDiff = cell.Column - topColumnIndex;
                    columnDiff = columnDiff % (firstColumnStripeHeight + secondColumnStripeHeight);
                    if (columnDiff < firstColumnStripeHeight)
                    {
                        return Style.FirstColumnStripe;
                    }
                }
                if (ShowColumnStripes && filter(Style.SecondColumnStripe))
                {
                    return Style.SecondColumnStripe;
                }
            }
            if (filter(Style.WholeTable))
            {
                return Style.WholeTable;
            }
            return null;
        }

    }
}